package com.xszx.cms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xszx.cms.entity.Banner;
import com.xszx.cms.service.BannerService;
import com.xszx.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 白熊
 * @Date: 2024/7/22 16:37
 */
@RestController
@RequestMapping("/cms/bannerAdmin")
//@CrossOrigin
@Api(tags = "banner后台管理")
public class BannerAdminController {
    @Autowired
    private BannerService bannerService;

    @ApiOperation(value = "获取Banner分页列表")
    @GetMapping("{page}/{limit}")
    public R index(
            @ApiParam(name = "page", value = "当前页码", required = true,defaultValue = "1")
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true,defaultValue = "2")
            @PathVariable Long limit) {
        Page<Banner> pageParam = new Page<>(page, limit);
        bannerService.pageBanner(pageParam,null);
        return R.ok().data("total", pageParam.getTotal())
                .data("records", pageParam.getRecords());
    }

    @ApiOperation(value = "根据获取Banner")
    @GetMapping("{id}")
    public R get(@PathVariable String id) {
        Banner banner = bannerService.getBannerById(id);
        return R.ok().data("item", banner);
    }

    @ApiOperation(value = "新增Banner")
    @PostMapping
    public R save(@RequestBody Banner banner) {
        bannerService.saveBanner(banner);
        return R.ok();
    }

    @ApiOperation(value = "修改Banner")
    @PutMapping("{id}")
    public R updateById(@PathVariable @ApiParam(value = "对象id",required = true)  String id ,
                        @RequestBody Banner banner) {
        bannerService.updateBannerById(banner);
        return R.ok();
    }

    @ApiOperation(value = "根据id删除Banner")
    @DeleteMapping("{id}")
    public R remove(@ApiParam(value = "对象id",required = true)
                        @PathVariable String id) {
        bannerService.removeBannerById(id);
        return R.ok();
    }
}
